package com.controller;

import com.enums.ExceptionEntity;
import com.enums.ResponseEntity;
import com.utils.UploalFiles;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import java.io.*;
import java.util.*;

/**
 * @Auth chengst
 * @Date Created in  18:59 2020/6/1
 */
@RestController
@RequestMapping("/api/upload")
public class UploadController {

    private static final Log log = LogFactory.getLog(UploadController.class);


    @PostMapping("/img")
    public Map upload(MultipartFile file) throws Exception {
        Map map = new HashMap<String, Object>();
        Map params = new HashMap<String, Object>();
        log.info(file);
        String newFolder = "web";
        String bucketName = "story";
        String upload = UploalFiles.upload(file, bucketName, newFolder);
        if (StringUtils.isNoneEmpty(upload)) {
            params.put("src", upload);
        }
        map.put("code", 0);
        map.put("message", "上传成功!");
        map.put("data", params);
        log.info(map);
        return map;
    }


    @PostMapping("/getFiles")
    public String getFiles(String files) throws Exception {
        String bucketName = "homepage";
        String upload = UploalFiles.getFiles(bucketName, files);
        return upload;
    }

    /**
     * 将字符串写入指定文件(当指定的父路径中文件夹不存在时，会最大限度去创建，以保证保存成功！)
     *
     * @param res 原字符串
     * @return 成功标记
     */
    @PostMapping("/uploadString")
    public static ExceptionEntity uploadString(String res, String fileName) throws Exception {
        ExceptionEntity resposne = new ExceptionEntity();
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        File distFile = new File(fileName);
        bufferedReader = new BufferedReader(new StringReader(res));
        bufferedWriter = new BufferedWriter(new FileWriter(distFile));
        char buf[] = new char[1024];         //字符缓冲区
        int len;
        while ((len = bufferedReader.read(buf)) != -1) {
            bufferedWriter.write(buf, 0, len);
        }
        bufferedWriter.flush();
        bufferedReader.close();
        bufferedWriter.close();
        if (bufferedReader != null) {
            bufferedReader.close();
        }
        //  将File 转为 MultipartFile
        MultipartFile multipartFile = new MockMultipartFile("file", fileName, "text/plain", new FileInputStream(distFile));
        //  上传到minio服务器
        String upload = UploalFiles.upload(multipartFile, "homepage", "book");
        System.out.println(upload);
        distFile.delete();
        resposne.setResponseEntity(ResponseEntity.CM_OK);
        return resposne;
    }


    @PostMapping("/upload2")
    @ResponseBody
    public String uploadFile(@RequestParam("files") MultipartFile[] files) throws Exception {
        List list = new LinkedList<>();
        for (MultipartFile file : files) {
            String upload = UploalFiles.upload(file, "homepage", "pic_img");
            list.add(upload);
        }
        log.info("多文件上传集合:" + list);
        return "success";
    }


    public static void convertBase64DataToImage(String base64ImgData, String filePath) throws IOException {
        BASE64Decoder d = new BASE64Decoder();
        byte[] bs = d.decodeBuffer(base64ImgData);
        FileOutputStream os = new FileOutputStream(filePath);
        os.write(bs);
        os.close();
        System.out.println(os);
    }

    public static void main(String[] args) throws IOException {
        convertBase64DataToImage("", "face");
    }
}
